package com.sicau.tools;

import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * QQ邮件发送工具类
 * 
 * 使用说明：
 * 1. 需要在QQ邮箱中开启SMTP服务并获取授权码
 * 2. 授权码不是QQ登录密码，需要在QQ邮箱设置中生成
 * 3. 该工具支持发送简单文本邮件
 */
@Component
public class QQEmailTool {

    @Resource
    private JavaMailSender sender;

    /**
     * 发送QQ邮件
     *
     * @param recipientEmail 收件人邮箱地址 (例如: recipient@example.com)
     * @param subject        邮件主题
     * @param content        邮件内容
     * @return 发送结果信息 ("邮件发送成功" 或 "邮件发送失败: 错误信息")
     * 
     * 使用示例：
     * QQEmailTool tool = new QQEmailTool();
     * String result = tool.sendQQEmail(
     *     "123456789@qq.com", 
     *     "your_auth_code", 
     *     "recipient@example.com", 
     *     "测试邮件主题", 
     *     "这是一封测试邮件内容"
     * );
     * System.out.println(result);
     */
    @Tool(description = "发送QQ邮件")
    public String sendQQEmail(
            @ToolParam(description = "收件人邮箱地址") String recipientEmail,
            @ToolParam(description = "邮件主题") String subject,
            @ToolParam(description = "邮件内容") String content) {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
        try {
            helper.addTo(recipientEmail);
            helper.setSubject(subject);
            helper.setText(content, true);
        } catch (MessagingException e) {
            return "邮件发送失败: " + e.getMessage();
        }
        sender.send(message);
        return "邮件发送成功";
    }
}